package com.zijie.shoprbacapi.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.zijie.core.util.JsonResult;
import com.zijie.rbac.model.Perm;
import com.zijie.rbac.model.Role;
import com.zijie.rbac.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/v1/roles", produces = MediaType.APPLICATION_JSON_VALUE)
public class RoleApi {
    private RoleService roleService;

    @Autowired
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    @GetMapping
    public ResponseEntity<JsonResult<?>> findAll(
                                                 @RequestParam(defaultValue = "1") Integer pageNo,
                                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                                 Role role) {
        Page<Role> page = new Page<>(pageNo, pageSize);
        page = roleService.findAll(role, page);
        return ResponseEntity.ok(JsonResult.success(page));
    }

    @GetMapping("/id/{id}")
    public ResponseEntity<JsonResult<?>> findById(@PathVariable("id") Integer id) {
        Role role = roleService.findById(id);
        if (role == null) {
            return ResponseEntity.ok(JsonResult.error("未查询到指定编号的角色"));
        } else {
            return ResponseEntity.ok(JsonResult.success(role));
        }
    }

    @PostMapping("/save")
    public ResponseEntity<JsonResult<?>> save(@RequestBody Role role) {
        boolean success = roleService.save(role);
        if (success) {
            return ResponseEntity.ok(JsonResult.success("保存成功"));
        } else {
            return ResponseEntity.ok(JsonResult.error("保存角色失败"));
        }
    }

    @PutMapping("/update")
    public ResponseEntity<JsonResult<?>> update(@RequestBody Role role) {
        boolean success = roleService.update(role);
        if (success) {
            return ResponseEntity.ok(JsonResult.success("修改成功"));
        } else {
            return ResponseEntity.ok(JsonResult.error("修改角色失败"));
        }
    }

    @DeleteMapping("/deleteById")
    public ResponseEntity<JsonResult<?>> deleteByIds(@RequestBody Integer[] ids) {
        int count = roleService.deleteByIds(List.of(ids));
        if (count > 0) {
            return ResponseEntity.ok(JsonResult.success(count));
        } else {
            return ResponseEntity.ok(JsonResult.error("删除角色失败"));
        }
    }
    //为用户组分配权限
    @PutMapping("/groupPerm")
    public ResponseEntity<JsonResult<?>> assignAdminToGroupForRole(
            @RequestParam("adminId") Long adminId,
            @RequestParam("userGroupId") Long userGroupId) {
        // 调用服务层执行分配操作
        boolean success = roleService.assignAdminToGroup(adminId, userGroupId);
        if (success) {
            return ResponseEntity.ok(JsonResult.success("分配成功"));
        } else {
            return ResponseEntity.ok(JsonResult.error("分配失败"));
        }
    }
    //为角色分配权限
    @PutMapping("/permId")
    public  ResponseEntity<JsonResult<?>> assignPermToRole(
            @RequestParam("roleId") Long roleId,
            @RequestParam("permId") Long permId) {
        boolean success = roleService.assignPermToRole(roleId, permId);
        if (success) {
            return ResponseEntity.ok(JsonResult.success("分配成功"));
        } else {
            return ResponseEntity.ok(JsonResult.error("分配失败"));
        }
    }
    //查询某个角色的权限
    @GetMapping("/{roleId}")
    public ResponseEntity<JsonResult<?>> findPermsByRoleId(@PathVariable Long roleId) {
        List<Perm> rolePermMappings = roleService.findPermsByRoleId(roleId);
        return ResponseEntity.ok(JsonResult.success(rolePermMappings));
    }
    //删除权限
    /**
     * 删除角色的某个权限
     *
     * @param roleId 角色ID
     * @param permissionId 权限ID
     * @return 操作结果
     */
    @DeleteMapping("/deleteByPermission")
    public ResponseEntity<JsonResult<?>>  removeRolePermission(
            @RequestParam("roleId") Long roleId,
            @RequestParam("permissionId") Long permissionId) {
        boolean result = roleService.removeRolePermission(roleId, permissionId);
        if (result) {
            return ResponseEntity.ok(JsonResult.success("删除权限成功"));
        } else {
            return ResponseEntity.ok(JsonResult.error("删除权限失败"));
        }
    }



}
